package com.se.nju.rtvps.dr;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class OpenDialog extends JDialog {
	public final static int ACTION_OPEN = 1;
	public final static int ACTION_CANCEL = 0;
	private String nameUrlDefault = null;
	private int intAction = ACTION_CANCEL;
	protected JFrame frameOwner = null;

	JPanel panelInput = new JPanel();
	GridLayout gridLayout1 = new GridLayout();
	FlowLayout flowLayout1 = new FlowLayout();
	JLabel jLabel1 = new JLabel();
	JTextField txtURL = new JTextField();
	JPanel panelCmd = new JPanel();
	JButton cmdOpen = new JButton();
	JButton cmdCancel = new JButton();

	public OpenDialog(JFrame frame, String nameUrlDefault) {
		super(frame, "Open Url", true);
		frameOwner = frame;
		this.nameUrlDefault = nameUrlDefault;
		try {
			jbInit();
			pack();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public OpenDialog() {
		this(null, "");
	}

	void jbInit() throws Exception {
		panelInput.setLayout(flowLayout1);
		this.setTitle("Open Url");
		this.getContentPane().setLayout(gridLayout1);
		gridLayout1.setRows(2);
		gridLayout1.setColumns(1);
		jLabel1.setForeground(Color.red);
		jLabel1.setText("URL:");
		cmdOpen.setBackground(new Color(180, 255, 50));
		cmdOpen.setForeground(Color.magenta);
		cmdOpen.setText("Open");
		txtURL.setText(nameUrlDefault);
		cmdOpen.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cmdOpen_actionPerformed(e);
			}
		});
		cmdCancel.setBackground(new Color(180, 255, 150));
		cmdCancel.setForeground(Color.magenta);
		cmdCancel.setText("Canel");
		cmdCancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cmdCancel_actionPerformed(e);
			}
		});
		panelCmd.setBackground(Color.orange);
		panelInput.setBackground(Color.orange);
		panelInput.add(jLabel1, null);
		panelInput.add(txtURL, null);
		txtURL.setColumns(30);
		this.getContentPane().add(panelInput, null);
		this.getContentPane().add(panelCmd, null);
		panelCmd.add(cmdOpen, null);
		panelCmd.add(cmdCancel, null);
		this.pack();
		this.autoPosition();
		this.setResizable(false);
	}

	public void addNotify() {
		this.setBackground(Color.lightGray);
		super.addNotify();
		autoPosition();
	}

	public void autoPosition() {
		Dimension dim;
		Dimension dimFrame;
		Dimension dimDialog;
		Dimension dimScreen;
		Point point;
		Insets insets;
		point = frameOwner.getLocationOnScreen();
		dimFrame = frameOwner.getSize();
		dimDialog = this.getSize();
		point.y = point.y
				+ (int) (dimFrame.getHeight() - dimDialog.getHeight()) / 2;
		this.setLocation(point);

	}

	public String getUrl() {
		String nameUrl;

		nameUrl = txtURL.getText();
		return (nameUrl);
	}

	void cmdOpen_actionPerformed(ActionEvent e) {
		this.setAction(this.ACTION_OPEN);
		this.dispose();
	}

	void cmdCancel_actionPerformed(ActionEvent e) {
		this.setAction(this.ACTION_CANCEL);
		this.dispose();
	}

	protected void setAction(int intAction) {
		this.intAction = intAction;
	}

	public int getAction() {
		return (intAction);
	}

}